package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.VendorProduct;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class VendorProductDAO extends AbstractDao<VendorProduct>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_product_vendor";
	public static final String DELETE_SQL = "DELETE FROM oce_product_vendor WHERE product_id = ? and vendor_id = ?";
	public static final String SELECT_PRODUCTS_SQL = "select * from oce_product_vendor where vendor_id = ?";
	public static final String SELECT_VENDORS_SQL = "select * from oce_product_vendor where product_id = ?";
	public static final String SELECT_SQL = "select * from oce_product_vendor where product_id = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_product_vendor (productVendorId,vendor_id,	product_id) VALUES (?,?,?)";
	
	public VendorProduct create(VendorProduct obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);	
			long id = 0l;
			ps.setLong(1,id);
			ps.setLong(2, Long.parseLong(obj.getVendorId()));
			ps.setLong(3, Long.parseLong(obj.getProductId()));				
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public VendorProduct update(VendorProduct obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public VendorProduct find(long id){
		List<VendorProduct> vendorproduct = new ArrayList<VendorProduct>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			vendorproduct = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return vendorproduct.size()== 0 ? null : vendorproduct.get(0);
	}
	
	public List<VendorProduct> findAll(){
		List<VendorProduct> vendorproduct = new ArrayList<VendorProduct>();
		try{
			vendorproduct = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			vendorproduct = null;
		}
		return vendorproduct;
	}
	
	public List<VendorProduct> findProductsByVendorId(long id){
		List<VendorProduct> vendorproduct = new ArrayList<VendorProduct>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_PRODUCTS_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			vendorproduct = convert(ConnectionDatabase.executeQuery(ps));			
		}catch (Exception e) {
			// TODO: handle exception
			vendorproduct = null;
		}
		return vendorproduct;
	}
	
	public List<VendorProduct> findVendorsByProductsId(long id){
		List<VendorProduct> vendorproduct = new ArrayList<VendorProduct>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_VENDORS_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			vendorproduct = convert(ConnectionDatabase.executeQuery(ps));			
		}catch (Exception e) {
			// TODO: handle exception
			vendorproduct = null;
		}
		return vendorproduct;
	}
	
	public void delete(VendorProduct obj){
		try{
			PreparedStatement ps = con.prepareStatement(DELETE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,Long.parseLong(obj.getProductId()));
			ps.setLong(2,Long.parseLong(obj.getVendorId()));
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();	
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<VendorProduct> convert(ResultSet rs) throws SQLException {
		List<VendorProduct> vendorproduct = new ArrayList<VendorProduct>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String vendorId = rs.getString(2);
	        String productId = rs.getString(3);
	        vendorproduct.add(new VendorProduct(id,vendorId,productId));
	    }
		rs.close();
		return vendorproduct;
	}
}
